﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.ComponentModel;

namespace DC2010_Editor
{
    public class CrystalButtonBase : Control, IAnimatedItem
    {
        // Fields
        private CrystalAnimator anim;
        protected float fadeProgress;
        internal a b;
        private DialogResult c;
        private bool d;
 



        [Flags]
        internal enum a
        {
            a = 0,
            b = 1,
            c = 2,
            d = 4
        }

        public CrystalButtonBase()
        {
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.CacheText | ControlStyles.SupportsTransparentBackColor | ControlStyles.StandardClick | ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
            base.SetStyle(ControlStyles.UserMouse | ControlStyles.UserPaint, true);
            base.ResizeRedraw = true;
        }

        private bool NeznamaMetoda(a A_0)
        {
            return ((this.b & A_0) == A_0);
        }

        private bool NeznamaMetoda(float A_0)
        {
            bool flag = false;
            this.fadeProgress += A_0;
            if (this.fadeProgress <= 0f)
            {
                this.fadeProgress = 0f;
                flag = true;
            }
            if (this.fadeProgress >= 1f)
            {
                this.fadeProgress = 1f;
                flag = true;
            }
            base.Invalidate();
            return !flag;
        }

        private void NeznamaMetoda(a A_0, bool A_1)
        {
            if (A_1)
            {
                this.b |= A_0;
                if (A_0 == a.b)
                {
                    this.Animator.FadeIn(this);
                }
            }
            else
            {
                if (A_0 == a.b)
                {
                    this.Animator.FadeOut(this);
                }
                this.b &= ~A_0;
            }
            foreach (string str in Enum.GetNames(typeof(a)))
            {
                a a1 = (a)Enum.Parse(typeof(a), str);
            }
        }

        protected virtual void CreateRegion()
        {
            int width = base.Width;
            int height = base.Height;
            Point[] points = new Point[] { new Point(1, 0), new Point(width - 1, 0), new Point(width - 1, 1), new Point(width, 1), new Point(width, height - 1), new Point(width - 1, height - 1), new Point(width - 1, height), new Point(1, height), new Point(1, height - 1), new Point(0, height - 1), new Point(0, 1), new Point(1, 1) };
            GraphicsPath path = new GraphicsPath();
            path.AddLines(points);
            base.Region = new Region(path);
        }

        public bool DecreaseFade()
        {
            return this.NeznamaMetoda(this.NeznamaMetoda(a.d) ? -0.1666667f : -0.1666667f);
        }

        public bool IncreaseFade()
        {
            return this.NeznamaMetoda(this.NeznamaMetoda(a.d) ? 0.1666667f : 0.3333333f);
        }

        public void NotifyDefault(bool value)
        {
            this.d = value;
            base.Invalidate();
        }

        protected override void OnClick(EventArgs e)
        {
            if (this.c != DialogResult.None)
            {
                base.FindForm().DialogResult = this.DialogResult;
            }
            base.OnClick(e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyData == Keys.Space)
            {
                if (!this.NeznamaMetoda(a.c))
                {
                    this.NeznamaMetoda(a.c, true);
                    base.Invalidate();
                }
                e.Handled = true;
            }
            base.OnKeyDown(e);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (this.NeznamaMetoda(a.c))
            {
                this.NeznamaMetoda(a.d, false);
                this.NeznamaMetoda(a.c, false);
                if ((e.KeyCode == Keys.Return) || (e.KeyCode == Keys.Space))
                {
                    this.OnClick(EventArgs.Empty);
                }
                e.Handled = true;
            }
            base.OnKeyUp(e);
        }

        protected override void OnLostFocus(EventArgs e)
        {
            this.NeznamaMetoda(a.c, false);
            base.OnLostFocus(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.NeznamaMetoda(a.c, true);
                this.NeznamaMetoda(a.d, true);
                base.Invalidate();
            }
            base.OnMouseDown(e);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            this.NeznamaMetoda(a.b, true);
            base.Invalidate();
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            this.NeznamaMetoda(a.b, false);
            base.Invalidate();
            base.OnMouseLeave(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if ((e.Button != MouseButtons.None) && this.NeznamaMetoda(a.d))
            {
                if (!base.ClientRectangle.Contains(e.X, e.Y))
                {
                    if (this.NeznamaMetoda(a.c))
                    {
                        this.NeznamaMetoda(a.c, false);
                        base.Invalidate();
                    }
                    if (this.NeznamaMetoda(a.b))
                    {
                        this.NeznamaMetoda(a.b, false);
                        base.Invalidate();
                    }
                }
                else
                {
                    if (!this.NeznamaMetoda(a.c))
                    {
                        this.NeznamaMetoda(a.c, true);
                        base.Invalidate();
                    }
                    if (!this.NeznamaMetoda(a.b))
                    {
                        this.NeznamaMetoda(a.b, true);
                        base.Invalidate();
                    }
                }
            }
            base.OnMouseMove(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            this.NeznamaMetoda(a.c, false);
            this.NeznamaMetoda(a.d, false);
            base.OnMouseUp(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.Clear(this.BackColor);
            this.PaintButton(e.Graphics);
        }

        protected override void OnResize(EventArgs e)
        {
            this.CreateRegion();
            base.OnResize(e);
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
        }

        protected virtual void PaintButton(Graphics g)
        {
        }

        public void PerformClick()
        {
            if (base.CanSelect)
            {
                base.OnClick(EventArgs.Empty);
            }
        }

        protected override bool ProcessMnemonic(char charCode)
        {
            if (Control.IsMnemonic(charCode, base.Text))
            {
                this.PerformClick();
                return true;
            }
            return base.ProcessMnemonic(charCode);
        }

        [Browsable(false)]
        protected CrystalAnimator Animator
        {
            get
            {
                if (this.anim == null)
                {
                    this.anim = new CrystalAnimator();
                }
                return this.anim;
            }
        }

 
        [Category("Appearance")]
        public DialogResult DialogResult
        {
            get
            {
                return this.c;
            }
            set
            {
                this.c = value;
            }
        }

        public float FadePercentage
        {
            get
            {
                return this.fadeProgress;
            }
        }

        protected bool MouseIsDown
        {
            get
            {
                return this.NeznamaMetoda(a.c);
            }
        }

        internal bool MouseIsOver
        {
            get
            {
                return this.NeznamaMetoda(a.b);
            }
        }

        internal bool MouseIsPressed
        {
            get
            {
                return this.NeznamaMetoda(a.d);
            }
        }
 

 

 

 


    }
}
